package org.example.controller;

import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.example.model.Constant;
import org.example.model.Result;
import org.example.model.UserInfo;
import org.example.service.UserService;
import org.example.utils.JwtUtils;
import org.example.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

@Slf4j
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    /**
     * 用户登录
     * @param username
     * @param password
     * @param request
     * @param response
     * @return
     */
    @PostMapping("/login")
    public Result login(String username, String password, HttpServletRequest request, HttpServletResponse response) {

        // 1. 校验参数合法性
        if (!StringUtils.hasLength(username) || !StringUtils.hasLength(password)) {
            log.error("username: " + username + " password: " + password);
            return Result.fail(Constant.RESULT_CODE_FAIL, "用户名或密码为空");
        }

        // 2. 校验用户密码是否正确
        UserInfo userInfo = userService.selectUserByName(username);
        if (userInfo == null) {
            log.error("用户不存在");
            return Result.fail(Constant.RESULT_CODE_FAIL, "用户不存在");
        }

        if (!SecurityUtils.verify(password, userInfo.getPassword())) {
            log.error("密码错误");
            return Result.fail(Constant.RESULT_CODE_FAIL, "密码错误");
        }

        // 3. 密码正确，返回token
        Map<String, Object> claim = new HashMap<>();
        claim.put(Constant.TOKEN_ID, userInfo.getId());
        claim.put(Constant.TOKEN_USERNAME, userInfo.getUserName());
        String token = JwtUtils.genJwt(claim);
        log.info("用户登录，返回结果token: {}", token);
        return Result.success(token);
    }

    /**
     * 获取当前登录用户信息
     */

    @GetMapping("/getLoginUserInfo")
    public Result getLoginUserInfo(HttpServletRequest request) {
        log.info("当前登录用户信息");
        // 获取token
        String token = request.getHeader(Constant.REQUEST_HEADER_TOKEN);
        // 从token中获取登录用户的ID
        Integer userId = JwtUtils.getUserIdFromToken(token);
        if (userId == null) {
            // 用户未登录
            return Result.unlogin();
        }
        UserInfo userInfo = userService.getUserById(userId);
        return Result.success(userInfo);
    }

    /**
     * 获取当前文章作者信息
     * @param blogId
     * @return
     */
    @GetMapping("/getAuthorInfo")
    public Result getAuthorInfo(Integer blogId) {
        log.info("获取当前文章id为: {} 的作者信息", blogId);
        if (blogId == null || blogId < 1) {
            return Result.fail(-1, "博客id不存在");
        }
        UserInfo userInfo = userService.getAuthorByBlogId(blogId);
        return Result.success(userInfo);
    }
}
